const express = require("express");

/* 业务依赖 */
const fs = require("fs");
const path = require("path")

const { uploadPath } = require("../config");


/* 创建路由器对象 */
const router = express.Router();

/* 定义路由 */
// POST /file/upload
router.post("/upload", (req, res) => {
    fs.readFile(req.files[0].path,(err,data)=>{
        console.log("data=",data);

        if(data){
            /* 如果读取成功，将其写入upload目录下 */
            fs.writeFile(
                path.join(uploadPath,req.files[0].filename),
                data,
                err=>{
                    err ? res.json(err) : res.json({
                        msg:"文件上传成功"
                    })
                }
            )
        }else{
            /* 读取文件失败 */
            res.json(err)
        }
    })
});

module.exports = router;
